/*
 * Copyright FUOC.  All rights reserved.

 */
package managedbean;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Properties;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import jpa.CustomerJPA;
import ejb.CustomerFacadeRemote;

/**
 * Managed Bean ModifyClientMBean
 */
@ManagedBean(name = "password")
@SessionScoped
public class ChangePasswordMBean implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@EJB
	private CustomerFacadeRemote customerRemote;

	public CustomerJPA dataCustomer = new CustomerJPA();
	private String user;
	private String oldPass;
	private String newPass1;
	private String newPass2;
	
	public ChangePasswordMBean(){
		//dataCustomer = (CustomerJPA) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
	}
	
	public CustomerJPA getDataCustomer()
	{
		return dataCustomer;
	}	
	public void setDataCustomer(CustomerJPA customer)
	{
		this.dataCustomer=customer;
	}
	
	public String modifyClient(){
		return "modifyClientView";
	}
	
	/**
	 * Methods to get the params from form.
	 */
 	public String getCustomerEmail(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("emailCustomer");
 	}
 	public String getCustomerPassword(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("passwordCustomer");
 	}
	public String getNewPassword1(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
		return params.get("newPass1");
 	}
	
	public String getNewPassword2(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("newPass2");
 	}
	
	/**
	 * Prepare data to change password
	 * @throws NamingException 
	 */
	public String changePasswordMBean() throws NamingException{
		FacesContext fc = FacesContext.getCurrentInstance();
		user = getCustomerEmail(fc);
		oldPass = getCustomerPassword(fc);
		newPass1 = getNewPassword1(fc);
		newPass2 = getNewPassword2(fc);
		if(!newPass1.equals(newPass2)){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "2 new passwords do not match!", null);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return "changePasswordView";
		}
		if(oldPass.isEmpty()||newPass1.isEmpty()||newPass2.isEmpty()){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have to fill all the gaps.", null);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return "changePasswordView";
		}
		if (!changePassword()){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Old password incorrect.", null);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return "changePasswordView";
		}
		return "modifyClientView";
	}
	
	/**
	 * Method to change password	
	 */
	public boolean changePassword() throws NamingException{
		Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		customerRemote = (CustomerFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/CustomerFacadeBean!ejb.CustomerFacadeRemote");
		return customerRemote.changePassword(user, newPass1, oldPass);
	}
	
	

}
